<template>
  <header class="nav">
    <div class="nav-inner">
      <div class="brand" @click="goHome">StreamVue</div>
      <form class="search" @submit.prevent="onSearch">
        <input
          v-model="query"
          type="text"
          placeholder="搜索你感兴趣的视频"
          aria-label="搜索视频"
        />
        <button type="submit">搜索</button>
      </form>
      <div class="account">
        <template v-if="currentUser">
          <div class="user-menu">
            <div class="user-info" @click="toggleMenu">
              <div class="avatar">{{ initials(currentUser.username) }}</div>
              <div class="user-details">
                <span class="name">{{ currentUser.username }}</span>
                <span v-if="currentUser.isMember" class="member-badge" :class="`member-${currentUser.memberLevel}`">
                  {{ memberLevelName }}
                </span>
              </div>
              <span class="dropdown-icon" :class="{ 'rotated': showMenu }">▼</span>
            </div>
            <div v-if="showMenu" class="dropdown-menu">
              <router-link to="/profile" class="menu-item" @click="closeMenu">
                <span class="menu-icon">👤</span> 个人中心
              </router-link>
              <router-link to="/member" class="menu-item" @click="closeMenu">
                <span class="menu-icon">💎</span> 会员中心
              </router-link>
              <button class="menu-item logout" @click="onLogout">
                <span class="menu-icon">🚪</span> 退出登录
              </button>
            </div>
          </div>
        </template>
        <template v-else>
          <router-link class="link" to="/login">登录</router-link>
          <span class="sep">/</span>
          <router-link class="link" to="/register">注册</router-link>
        </template>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { getCurrentUser, logout } from '@/utils/auth'

const router = useRouter()
const query = ref('')
const showMenu = ref(false)

const goHome = () => router.push('/')
const onSearch = () => router.push({ path: '/', query: { q: query.value.trim() } })

// 使用 ref + 事件监听，确保登录后立即更新
const currentUser = ref(getCurrentUser())
const updateUser = () => { currentUser.value = getCurrentUser() }

// 会员等级名称
const memberLevelName = computed(() => {
  if (!currentUser.value?.isMember) return ''
  const levels = {
    standard: '标准会员',
    premium: '高级会员'
  }
  return levels[currentUser.value.memberLevel] || '会员'
})

// 切换下拉菜单
function toggleMenu() {
  showMenu.value = !showMenu.value
}

// 关闭下拉菜单
function closeMenu() {
  showMenu.value = false
}

// 点击页面其他地方关闭菜单
function handleClickOutside(event) {
  const userMenu = document.querySelector('.user-menu')
  if (userMenu && !userMenu.contains(event.target)) {
    closeMenu()
  }
}

onMounted(() => {
  window.addEventListener('auth-changed', updateUser)
  window.addEventListener('storage', updateUser)
  window.addEventListener('click', handleClickOutside)
})
onUnmounted(() => {
  window.removeEventListener('auth-changed', updateUser)
  window.removeEventListener('storage', updateUser)
  window.removeEventListener('click', handleClickOutside)
})

function initials(name) { return (name || '?').slice(0, 1).toUpperCase() }
function onLogout() { logout(); updateUser(); router.push('/'); closeMenu() }
</script>

<style scoped>
.nav {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(15, 17, 21, 0.85);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  backdrop-filter: saturate(1.2) blur(12px);
  transition: all 0.3s ease;
}

.nav:hover {
  box-shadow: var(--shadow);
  background: rgba(15, 17, 21, 0.9);
}

.nav-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 12px 16px;
  display: grid;
  grid-template-columns: 160px 1fr auto;
  align-items: center;
  gap: 20px;
}

/* 品牌名称样式 */
.brand {
  font-weight: 800;
  font-size: 1.25rem;
  letter-spacing: 0.5px;
  cursor: pointer;
  background: linear-gradient(90deg, var(--accent), #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 0 10px rgba(99, 102, 241, 0.3);
  transition: all 0.3s ease;
}

.brand:hover {
  transform: scale(1.02);
  text-shadow: 0 0 15px rgba(99, 102, 241, 0.5);
}

/* 搜索栏样式 */
.search {
  display: flex;
  gap: 8px;
  justify-self: center;
  width: 100%;
  max-width: 500px;
}

.search input {
  flex: 1;
  min-width: 0;
  padding: 10px 16px;
  border-radius: var(--radius-full);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--fg);
  outline: none;
  transition: all 0.3s ease;
  font-size: 14px;
}

.search input::placeholder {
  color: var(--muted);
}

.search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
  transform: translateY(-1px);
}

.search button {
  padding: 8px 16px;
  border-radius: var(--radius-full);
  background: linear-gradient(90deg, var(--accent), #8b5cf6);
  border: none;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 600;
  position: relative;
  overflow: hidden;
}

.search button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: left 0.5s ease;
}

.search button:hover {
  transform: translateY(-1px);
  box-shadow: var(--glow-soft);
}

.search button:hover::before {
  left: 100%;
}

.search button:active {
  transform: translateY(0) scale(0.98);
}

/* 账号区域样式 */
.account {
  display: flex;
  align-items: center;
  gap: 12px;
  justify-self: end;
}

.user-menu {
  position: relative;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: var(--radius-full);
  background: var(--surface);
  border: 1px solid var(--border);
  color: inherit;
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-info:hover {
  background: var(--surface-hover);
  border-color: var(--accent);
  transform: translateY(-1px);
}

.user-details {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent-light);
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  transition: all 0.3s ease;
}

.user-info:hover .avatar {
  box-shadow: var(--glow);
}

.name {
  font-weight: 600;
  font-size: 14px;
}

.member-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 10px;
  white-space: nowrap;
}

.member-standard {
  background: #00a1d6;
  color: white;
}

.member-premium {
  background: #8b5cf6;
  color: white;
}

.dropdown-icon {
  font-size: 10px;
  transition: transform 0.3s ease;
}

.dropdown-icon.rotated {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 4px;
  background: rgba(15, 17, 21, 0.95);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  min-width: 160px;
  z-index: 100;
  backdrop-filter: blur(10px);
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  color: var(--fg);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
}

.menu-item:hover {
  background: var(--surface-hover);
}

.menu-item:first-child {
  border-radius: 8px 8px 0 0;
}

.menu-item:last-child {
  border-radius: 0 0 8px 8px;
}

.menu-icon {
  font-size: 16px;
  width: 16px;
  text-align: center;
}

.logout {
  color: var(--danger);
}

.logout:hover {
  background: rgba(239, 68, 68, 0.1);
}

.link {
  color: var(--accent);
  padding: 6px 12px;
  border-radius: var(--radius-full);
  transition: all 0.3s ease;
  font-weight: 500;
  font-size: 14px;
}

.link:hover {
  background: var(--accent-light);
  color: var(--accent-hover);
  transform: translateY(-1px);
}

.sep {
  color: var(--muted);
  font-size: 18px;
}

/* 移动端响应式 */
@media (max-width: 768px) {
  .nav-inner {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .search {
    justify-self: stretch;
    max-width: 100%;
  }
  
  .account {
    justify-self: center;
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .brand {
    justify-self: center;
  }
}
</style>